var mysql = require('mysql');
var async = require('async');
var pool = mysql.createPool({
    host: '123.56.165.191',
    user: 'lanny',
    password: 'BD78F921',
    database: 'ultrax', // 前面建的user表位于这个数据库中
    port: 3306
});
function getLength(str) {
    str = String(str);
    if (str.length <= 1) {
        str = '0' + str;
    }
    return str;
}
function getTime(date) {
    if ('Invalid Date' == date) {
        date = new Date();
    }
    return date.getFullYear() + '-' + getLength(date.getMonth() + 1) +
        '-' + getLength(date.getDate()) + ' ' + getLength(date.getHours()) + ':' + getLength(date.getMinutes()) + ':' + getLength(date.getSeconds());
}
var MBKTO = {
    'k': '1000',
    'm': '1000000',
    'b': '1000000000'
};
function getMBK(str) {
    var arr = str.split(' ');
    var head = arr[0].substr(1, arr[0].length);
    var headArr = head.split(',');
    var str = '';
    for (var i = 0; i < headArr.length; i++) {
        str += headArr[i];
    }
    var mbkL = MBKTO[arr[1].toLowerCase()];
    return Number(str * mbkL);
}
function getMBK2(str) {
    if (!str)return 0;
    var head = str.substr(0, str.length - 1);
    var headArr = head.split(',');
    var ss = '';
    for (var i = 0; i < headArr.length; i++) {
        ss += headArr[i];
    }
    var unit = str.substr(str.length - 1, 1);
    var mbkL = 1;
    if (isNaN(unit)) {
        mbkL = MBKTO[str.substr(str.length - 1, 1).toLowerCase()];
    }
    return Number(ss * mbkL);
}
function getNumber(str) {
    var arr = str.split(',');
    var str = '';
    for (var i = 0; i < arr.length; i++) {
        str += arr[i];
    }
    return Number(str);
}
function SqlDo(obj) {
    console.log('存库');
    pool.getConnection(function (err, connection) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('连接成功');
        async.mapLimit(obj, 4, function (item, callback) {
            addBasic(item, connection, callback);
        }, function (err, data) {
            if (err) {
                console.log(err);
                return;
            }
            console.log(data);
            // 释放连接
            connection.release();
        });
    });
}
var sqlBasicSearch = 'SELECT * FROM `etf_basic_info` WHERE `etf_symbol`=?';
var sqlBasicUpdate = 'UPDATE `etf_basic_info` SET `etf_symbol`=?,`name_cn`=?,`name_en`=?,`introduction`=?,`cur_type`=?,`stock_site`=?,`country`=?,`issuer`=?,`issue_date`=?,`index`=?,`etf_type`=?,`go_long`=?,`is_lever`=?,`is_hedging`=?,`src`=?,`src_id`=? WHERE `etf_id`=?';
// var sqlBasic = 'INSERT INTO `etf_basic_info`(`etf_id`, `etf_symbol`, `name_cn`, `name_en`, `introduction`, `cur_type`, `stock_site`, `country`, `issuer`, `issue_date`, `index`, `etf_type`, `go_long`, `is_lever`, `is_hedging`) VALUES(0,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
var sqlBasicNew = 'INSERT INTO `etf_basic_info`(`etf_id`, `etf_symbol`, `name_cn`, `name_en`, `introduction`, `cur_type`, `stock_site`, `country`, `issuer`, `issue_date`, `index`, `etf_type`, `go_long`, `is_lever`, `is_hedging`, `src`, `src_id`) VALUES(0,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
function addBasic(obj, conn, cb) {
    if (!obj || obj.symbol == null) {
        console.log('内容为空');
        cb(null, '');
        return;
    }
    console.log(obj.symbol);
    var sqlSearchId = '';
    var getSqlSearchId = '0';
    var arr = [];
    arr.push(obj.symbol);
    arr.push(obj.name_cn);
    arr.push(obj.name_en);
    arr.push(obj.introduction || '');
    arr.push(obj.cur_type || '美元');
    arr.push(obj.stock_site || '');
    arr.push(obj.country);
    arr.push(obj.issuer);
    var date = new Date(obj.issue_date);
    arr.push(getTime(date));
    arr.push(obj.index);
    arr.push(obj.etf_type);
    arr.push(obj.go_long);
    var is_lever = 0;
    if (obj.is_lever != '无' && obj.is_lever != '否') {
        is_lever = obj.is_lever;
    }
    if (obj.is_lever == '是') {
        is_lever = 1;
    }
    arr.push(is_lever);
    var is_hedging = 0;
    if (obj.is_hedging != '无') {
        is_hedging = 1;
    }
    arr.push(is_hedging);
    arr.push(obj.src);
    conn.query(sqlBasicSearch, [obj.symbol], function (err, result) {
        if (err) {
            console.log(err);
            cb(err);
            return;
        }
        if (result.length > 0) {
            if (obj.src == '4' || obj.src == '3') {
                arr.push('0');
                arr.push(result[0].etf_id);
                conn.query(sqlBasicUpdate, arr, function (er, res) {
                    if (er) {
                        console.log(er);
                        cb(er);
                        return;
                    }
                    addFee(result[0].etf_id, conn, obj, cb);
                });
                return;
            }
            if (obj.src == '0') {
                sqlSearchId = 'SELECT * FROM `country_info` WHERE `country_cn`=?';
            } else if (obj.src == '2') {
                sqlSearchId = 'SELECT * FROM `commodity` WHERE `symbol_cn`=?';
            }
            conn.query(sqlSearchId, [obj.country], function (err, rult) {
                if (err) {
                    cb(err);
                    return;
                }
                if (rult.length > 0) {
                    if (obj.src == '0') {
                        getSqlSearchId = rult[0].country_id;
                    } else if (obj.src == '2') {
                        getSqlSearchId = rult[0].id;
                    }
                }
                arr.push(getSqlSearchId);
                arr.push(result[0].etf_id);
                conn.query(sqlBasicUpdate, arr, function (er, res) {
                    if (er) {
                        console.log(er);
                        cb(er);
                        return;
                    }
                    addFee(result[0].etf_id, conn, obj, cb);
                });
            });
        } else {
            if (obj.src == 4 || obj.src == '3') {
                arr.push('0');
                conn.query(sqlBasicNew, arr, function (errr, ress) {
                    if (errr) {
                        console.log(errr);
                        cb(errr);
                        return;
                    }
                    addFee(ress.insertId, conn, obj, cb);
                });
                return;
            }
            if (obj.src == '0') {
                sqlSearchId = 'SELECT * FROM `country_info` WHERE `country_cn`=?';
            } else if (obj.src == '2') {
                sqlSearchId = 'SELECT * FROM `commodity` WHERE `symbol_cn`=?';
            }
            conn.query(sqlSearchId, [obj.country], function (err, rult) {
                if (err) {
                    cb(err);
                    return;
                }
                if (rult.length > 0) {
                    if (obj.src == '0') {
                        getSqlSearchId = rult[0].country_id;
                    } else if (obj.src == '2') {
                        getSqlSearchId = rult[0].id;
                    }
                }
                arr.push(getSqlSearchId);
                conn.query(sqlBasicNew, arr, function (errr, ress) {
                    if (errr) {
                        console.log(errr);
                        cb(errr);
                        return;
                    }
                    addFee(ress.insertId, conn, obj, cb);
                });
            });
        }
    });
}
var sqlFeeSearch = 'SELECT * FROM `etf_fee_info` WHERE `etf_id`=?';
var sqlFeeUpdate = 'UPDATE `etf_fee_info` SET `etf_symbol`=?,`time`=?,`ratio`=?,`num`=?,`1m_vol`=?,`3m_vol`=?,`pe_ratio`=?,`yield`=?,`num_holding`=? WHERE `id`=?';
var sqlFee = 'INSERT INTO `etf_fee_info`( `id`, `etf_id`, `etf_symbol`, `time`, `ratio`, `num`, `1m_vol`, `3m_vol`, `pe_ratio`, `yield`, `num_holding`) VALUES(0,?,?,?,?,?,?,?,?,?,?)';
function addFee(id, conn, obj, cb) {
    console.log('addFee' + obj.symbol);
    var arr = [];
    arr.push(obj.symbol);
    var date = new Date(obj.time);
    //var date = new Date();
    arr.push(getTime(date));
    arr.push(obj.ratio);
    arr.push(getMBK(obj.aum));
    arr.push(getNumber(obj['1m_vol']));
    arr.push(getNumber(obj['3m_vol']));
    arr.push(obj.pe_ratio);
    arr.push(obj.yield);
    arr.push(Number(obj.num_holding));
    //console.log(arr);
    conn.query(sqlFeeSearch, [id], function (err, result) {
        if (err) {
            console.log(err);
            cb(err);
            return;
        }
        if (result.length > 0) {
            arr.push(result[0].id);
            conn.query(sqlFeeUpdate, arr, function (er, res) {
                if (er) {
                    console.log(er);
                    cb(er);
                    return;
                }
                addTopTen(id, conn, obj, cb);
            });
        } else {
            arr.unshift(id);
            conn.query(sqlFee, arr, function (errr, ress) {
                if (errr) {
                    console.log(errr);
                    cb(errr);
                    return;
                }
                addTopTen(id, conn, obj, cb);
            });
        }
    });
}
var sqlTopTenDel = 'DELETE FROM `etf_topten` WHERE `etf_id`=?';
var sqlTopTen = 'INSERT INTO `etf_topten`(`id`,`etf_id`,`etf_symbol`, `time`, `cpy_name`, `cpy_code`, `percent`) VALUES(0,?,?,?,?,?,?)';
function addTopTen(id, conn, obj, cb) {
    console.log('addTopTen' + obj.symbol);
    conn.query(sqlTopTenDel, [id], function (err, result) {
        if (err) {
            cb(err);
            return;
        }
        async.mapLimit(obj.top_ten, 4, function (item, call1) {
            var arr = [];
            arr.push(id);
            arr.push(obj.symbol);
            var date = new Date(obj.issue_date);
            arr.push(getTime(date));
            //arr.push(obj.symbol);
            //arr.push(new Date().getTime().toString());
            arr.push(item.cpy_name);
            arr.push(item.cpy_code);
            arr.push(item.percent);
            conn.query(sqlTopTen, arr, function (err, result) {
                if (err) {
                    call1(err);
                }
                call1(null, result.insertId);
            });
        }, function (err, data) {
            if (err) {
                cb(err);
                return;
            }
            addSector(id, conn, obj, cb);
        });
    });
}
var sqlSectorDel = 'DELETE FROM `etf_sector` WHERE `etf_id`=?';
var sqlSector = 'INSERT INTO `etf_sector`(`id`,`etf_id`,`etf_symbol`, `time`, `sec_name`, `percent`) VALUES(0,?,?,?,?,?)';
function addSector(id, conn, obj, cb) {
    console.log('addSector' + obj.symbol);
    conn.query(sqlSectorDel, [id], function (err, result) {
        if (err) {
            cb(err);
            return;
        }
        async.mapLimit(obj.sector, 4, function (item, call1) {
            var arr = [];
            arr.push(id);
            arr.push(obj.symbol);
            var date = new Date(obj.issue_date);
            arr.push(getTime(date));
            arr.push(item.sec_name);
            arr.push(item.percent);
            conn.query(sqlSector, arr, function (err, result) {
                if (err) {
                    call1(err);
                }
                call1(null, result.insertId);
            });
        }, function (err, data) {
            if (err) {
                cb(err);
                return;
            }
            addCap(id, conn, obj, cb);
        });
    });
}
var sqlCapDel = 'DELETE FROM `etf_mkt_cap` WHERE `etf_id`=?';
var sqlCap = 'INSERT INTO `etf_mkt_cap`(`id`,`etf_id`,`etf_symbol`, `time`, `mkt_name`, `percent`)  VALUES(0,?,?,?,?,?)';
function addCap(id, conn, obj, cb) {
    console.log('addCap' + obj.symbol);
    conn.query(sqlCapDel, [id], function (err, result) {
        if (err) {
            cb(err);
            return;
        }
        async.mapLimit(obj.market_cap, 4, function (item, call1) {
            var arr = [];
            arr.push(id);
            //arr.push(obj.symbol);
            //arr.push(new Date().getTime().toString());
            arr.push(obj.symbol);
            var date = new Date(obj.issue_date);
            arr.push(getTime(date));
            arr.push(item.mkt_name);
            arr.push(item.percent);
            conn.query(sqlCap, arr, function (err, result) {
                if (err) {
                    call1(err);
                }
                call1(null, result.insertId);
            });
        }, function (err, data) {
            if (err) {
                cb(err);
                return;
            }
            addRealtime(id, conn, obj, cb);
        });
    });
}
var sqlRealtimeSearch = 'SELECT * FROM `etf_realtime_data` WHERE `etf_id`=? and `time`=?';
var sqlRealtimeUpdate = 'UPDATE `etf_realtime_data` SET `return1w`=?,`return1m`=?,`return3m`=?,`return1y`=?,`return3y`=?,`return5y`=?,`rtn_half_year`=?,`ytd`=? WHERE `id`=?';
var sqlRealtimeByNow = 'INSERT INTO `etf_realtime_data`(`id`, `etf_id`, `symbol`, `name`, `open`, `high`,`52_high`, `low`,`52_low`, `last`, `volume`, `previousclose`, `change`, `changeinpercent`, `time`,`dateTime`, `mktclose`) VALUES (0,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';

function addRealtime(id, conn, obj, cb) {
    console.log('addRealtime' + obj.symbol);
    conn.query('SELECT * FROM `etf_profit` WHERE `etf_id`=?', [id], function (err, result) {
        if (err) {
            console.log(err);
            cb(err);
            return;
        }
        if (result.length > 0) {
            var arr = [];
            //var date = new Date(obj.issue_date);
            //arr.push(getTime(date));
            arr.push(obj.time);
            arr.push(obj.returnData[0].value);
            arr.push(obj.returnData[1].value);
            arr.push(obj.returnData[2].value);
            arr.push(obj.returnData[5].value);
            arr.push(obj.returnData[6].value);
            arr.push(obj.returnData[7].value);
            arr.push(obj.returnData[3].value);
            arr.push(obj.returnData[4].value);
            conn.query('SELECT * FROM `etf_realtime_data` WHERE `etf_id`=? ORDER BY `dateTime` DESC LIMIT 1', [id], function (er, ress) {
                if (er) {
                    cb(er);
                    return;
                }
                if (ress.length > 0) {
                    arr.push(ress[0].changeinpercent);
                    arr.push(result[0].id);
                    conn.query('UPDATE `etf_profit` SET `dateTime`=?,`return1w`=?,`return1m`=?,`return3m`=?,`return1y`=?,`return3y`=?,`return5y`=?,`rtn_half_year`=?,`ytd`=?,`changeinpercent`=? WHERE `id`=?', arr, function (er, res) {
                        if (er) {
                            cb(er);
                            return;
                        }
                        cb(null, '成功');
                    });
                } else {
                    arr.push('0');
                    arr.push(result[0].id);
                    conn.query('UPDATE `etf_profit` SET `dateTime`=?,`return1w`=?,`return1m`=?,`return3m`=?,`return1y`=?,`return3y`=?,`return5y`=?,`rtn_half_year`=?,`ytd`=?,`changeinpercent`=? WHERE `id`=?', arr, function (er, res) {
                        if (er) {
                            cb(er);
                            return;
                        }
                        cb(null, '成功');
                    });
                }
            });
        } else {
            var ar = [];
            ar.push(id);
            ar.push(obj.symbol);
            ar.push(obj.time);
            ar.push(obj.returnData[0].value);
            ar.push(obj.returnData[1].value);
            ar.push(obj.returnData[2].value);
            ar.push(obj.returnData[5].value);
            ar.push(obj.returnData[6].value);
            ar.push(obj.returnData[7].value);
            ar.push(obj.returnData[3].value);
            ar.push(obj.returnData[4].value);
            conn.query('INSERT INTO `etf_profit`(`id`, `etf_id`, `symbol`, `dateTime`, `return1w`, `return1m`, `return3m`, `return1y`, `return3y`, `return5y`, `rtn_half_year`, `ytd`) VALUES (0,?,?,?,?,?,?,?,?,?,?,?)', ar, function (errr, ress) {
                if (errr) {
                    console.log(errr);
                    cb(errr);
                    return;
                }
                cb(null, '成功');
            });
        }
    });
}
module.exports.todo = SqlDo;
var sqlHistoryDel = 'DELETE FROM `etf_xxx_history_data` WHERE 1';
var sqlHistory = 'INSERT INTO `etf_xxx_history_data`(`id`, `etf_id`, `symbol`, `name`, `open`, `high`, `low`, `close`, `date`, `vol`) VALUES (0,?,?,?,?,?,?,?,?,?)';
var flagArr = ['daily', 'weekly', 'monthly'];
function getArr(arr) {
    var ar = [];
    for (var i = 0; i < arr.length; i++) {
        ar = ar.concat(arr[i]);
    }
    return ar;
}
module.exports.toHistoryDaily = function (arr) {
    console.log('历史天存库');
    pool.getConnection(function (err, connection) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('连接成功');
        // 注释为重置数据库使用
        //connection.query(sqlHistoryDel.replace('xxx', 'daily'), function (err, result) {
        //    if (err) {
        //        console.log(err);
        //        return;
        //    }
        async.mapLimit(arr, 4, function (it, cb) {
            console.log(it.symbol);
            var arr = [];
            arr.push(it.symbol);
            arr.push(it.name);
            arr.push(it.open);
            arr.push(it.high);
            arr.push(it.low);
            arr.push(it.close);
            arr.push(it.date);
            arr.push(getMBK2(it.volume));
            connection.query(sqlBasicSearch, [it.symbol], function (err, result) {
                if (err) {
                    console.log(err);
                    cb(err);
                    return;
                }
                if (result.length > 0) {
                    arr.unshift(result[0].etf_id);
                    connection.query('SELECT * FROM `etf_daily_history_data` WHERE `etf_id`=? AND `date` =?', [arr[0], arr[7]], function (err, res) {
                        if (err) {
                            cb(err);
                            return;
                        }
                        if (res.length <= 0) {
                            connection.query(sqlHistory.replace('xxx', 'daily'), arr, function (err, ress) {
                                if (err) {
                                    cb(err);
                                    return;
                                }
                                cb(null, 'ok');
                            });
                        } else {
                            var updateArr = [];
                            updateArr.push(it.open);
                            updateArr.push(it.high);
                            updateArr.push(it.low);
                            updateArr.push(it.close);
                            updateArr.push(getMBK2(it.volume));
                            updateArr.push(res[0].id);
                            connection.query('UPDATE `etf_daily_history_data` SET `open`=?,`high`=?,`low`=?,`close`=?,`vol`=? WHERE `id`=?', updateArr, function (eeee, rrrr) {
                                if (eeee) {
                                    cb(eeee);
                                    return;
                                }
                                cb(null, 'ok');
                            });
                        }
                    });
                } else {
                    cb(null, 'ok');
                }
            });
        }, function (err, data) {
            if (err) {
                console.log(err);
                return;
            }
            console.log(data);
            // 释放连接
            connection.release();
        });
    });
    //});
};
module.exports.toHistoryWeekly = function (arr) {
    console.log('历史周存库');
    pool.getConnection(function (err, connection) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('连接成功');
        // 注释为重置数据库使用
        //connection.query(sqlHistoryDel.replace('xxx', 'weekly'), function (err, result) {
        //    if (err) {
        //        console.log(err);
        //        return;
        //    }
        async.mapLimit(arr, 4, function (it, cb) {
            console.log(it.symbol);
            var arr = [];
            arr.push(it.symbol);
            arr.push(it.name);
            arr.push(it.open);
            arr.push(it.high);
            arr.push(it.low);
            arr.push(it.close);
            arr.push(it.date);
            arr.push(getMBK2(it.volume));
            connection.query(sqlBasicSearch, [it.symbol], function (err, result) {
                if (err) {
                    console.log(err);
                    cb(err);
                    return;
                }
                if (result.length > 0) {
                    arr.unshift(result[0].etf_id);
                    connection.query('SELECT * FROM `etf_weekly_history_data` WHERE `etf_id`=? AND `date` =?', [arr[0], arr[7]], function (err, res) {
                        if (err) {
                            cb(err);
                            return;
                        }
                        if (res.length <= 0) {
                            connection.query(sqlHistory.replace('xxx', 'weekly'), arr, function (err, ress) {
                                if (err) {
                                    cb(err);
                                    return;
                                }
                                cb(null, 'ok');
                            });
                        } else {
                            var updateArr = [];
                            updateArr.push(it.open);
                            updateArr.push(it.high);
                            updateArr.push(it.low);
                            updateArr.push(it.close);
                            updateArr.push(getMBK2(it.volume));
                            updateArr.push(res[0].id);
                            connection.query('UPDATE `etf_weekly_history_data` SET `open`=?,`high`=?,`low`=?,`close`=?,`vol`=? WHERE `id`=?', updateArr, function (eeee, rrrr) {
                                if (eeee) {
                                    cb(eeee);
                                    return;
                                }
                                cb(null, 'ok');
                            });
                        }
                    });
                } else {
                    cb(null, 'ok');
                }
            });
        }, function (err, data) {
            if (err) {
                console.log(err);
                return;
            }
            console.log(data);
            // 释放连接
            connection.release();
        });
    });
    //});
};
module.exports.toHistoryMonthly = function (arr) {
    console.log('历史月份存库');
    pool.getConnection(function (err, connection) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('连接成功');
        // 注释为重置数据库使用
        //connection.query(sqlHistoryDel.replace('xxx', 'monthly'), function (err, result) {
        //    if (err) {
        //        console.log(err);
        //        return;
        //    }
        async.mapLimit(arr, 4, function (it, cb) {
            console.log(it.symbol);
            var arr = [];
            arr.push(it.symbol);
            arr.push(it.name);
            arr.push(it.open);
            arr.push(it.high);
            arr.push(it.low);
            arr.push(it.close);
            arr.push(it.date);
            arr.push(getMBK2(it.volume));
            connection.query(sqlBasicSearch, [it.symbol], function (err, result) {
                if (err) {
                    console.log(err);
                    cb(err);
                    return;
                }
                if (result.length > 0) {
                    arr.unshift(result[0].etf_id);
                    connection.query('SELECT * FROM `etf_monthly_history_data` WHERE `etf_id`=? AND `date` =?', [arr[0], arr[7]], function (err, res) {
                        if (err) {
                            cb(err);
                            return;
                        }
                        if (res.length <= 0) {
                            connection.query(sqlHistory.replace('xxx', 'monthly'), arr, function (err, ress) {
                                if (err) {
                                    cb(err);
                                    return;
                                }
                                cb(null, 'ok');
                            });
                        } else {
                            var updateArr = [];
                            updateArr.push(it.open);
                            updateArr.push(it.high);
                            updateArr.push(it.low);
                            updateArr.push(it.close);
                            updateArr.push(getMBK2(it.volume));
                            updateArr.push(res[0].id);
                            connection.query('UPDATE `etf_monthly_history_data` SET `open`=?,`high`=?,`low`=?,`close`=?,`vol`=? WHERE `id`=?', updateArr, function (eeee, rrrr) {
                                if (eeee) {
                                    cb(eeee);
                                    return;
                                }
                                cb(null, 'ok');
                            });
                        }
                    });
                } else {
                    cb(null, 'ok');
                }
            });
        }, function (err, data) {
            if (err) {
                console.log(err);
                return;
            }
            console.log(data);
            // 释放连接
            connection.release();
        });
    });
    //});
};
var sqlRealtimeUpdateByNow = 'UPDATE `etf_realtime_data` SET `name`=?,`open`=?,`high`=?,`low`=?,`last`=?,`volume`=?,`previousclose`=?,`change`=?,`changeinpercent`=?,`time`=?,`dateTime`=?,`mktclose`=? WHERE `id`=?';
module.exports.toRealTime = function (arr) {
    console.log('存库');
    pool.getConnection(function (err, conn) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('数据库连接成功');
        console.log('开始循环插入数据库');
        async.mapLimit(arr, 3, function (obj, cb) {
            conn.query(sqlBasicSearch, [obj.symbol], function (err, res) {
                if (err) {
                    console.log(err);
                    cb(err);
                    return;
                }
                console.log('获取当前etf标示的唯一id');
                console.log(res);
                if (res.length > 0) {
                    console.log('根据唯一id及当前时间获取是否已经存在该数据');
                    console.log(res[0].etf_id);
                    console.log(obj.time);
                    conn.query(sqlRealtimeSearch, [res[0].etf_id, obj.time], function (err, result) {
                        if (err) {
                            console.log(err);
                            cb(err);
                            return;
                        }
                        console.log('是否存在当前时间段');
                        console.log(result);
                        if (result.length > 0) {
                            cb(null, '成功');
                        } else {
                            console.log('不存在当前时间段，插入数据');
                            var ar = [];
                            ar.push(res[0].etf_id);
                            ar.push(obj.symbol);
                            ar.push(obj.name || '');
                            ar.push(obj.open);
                            ar.push(Number(obj.high));
                            ar.push(Number(obj['52_high']));
                            ar.push(Number(obj.low));
                            ar.push(Number(obj['52_low']));
                            if (obj.closed == 0) {
                                ar.push((obj.last || obj.previousclose));
                            } else {
                                ar.push(obj.last);
                            }
                            ar.push(getMBK2(obj.volume));
                            ar.push(obj.previousclose);
                            ar.push(obj.change);
                            ar.push(obj.changeinpercent);
                            ar.push(obj.time);
                            var date = new Date().getTime() + 8 * 3600000;
                            ar.push(getTime(new Date(date)));
                            ar.push(obj.closed);
                            console.log('插入的数组为:');
                            console.log(ar);
                            conn.query(sqlRealtimeByNow, ar, function (errr, ress) {
                                if (errr) {
                                    console.log(errr);
                                    cb(errr);
                                    return;
                                }
                                cb(null, '成功');
                            });
                        }
                    });
                } else {
                    cb(null, '成功');
                }
            });
        }, function (err, data) {
            if (err) {
                console.log(err);
                console.log('realtime 部分存库失败');
                conn.release();
                return;
            }
            console.log('realtime 存库完成');
            // 释放连接
            conn.release();
        });
    });
};
module.exports.removeRealTime = function () {
    console.log('删除一个月前数据');
    pool.getConnection(function (err, conn) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('数据库连接成功');
        // 计算出三十天之前的日期
        var date = new Date().getTime() + 8 * 3600000 - 2592000000;
        var time = getTime(new Date(date));
        conn.query('DELETE FROM `etf_realtime_data` WHERE `dateTime` < ?', [time], function (err, res) {
            if (err) {
                console.log(err);
                conn.release();
                return;
            }
            console.log('删除一个月前数据成功');
            conn.release();
        });
    });
};